/*
 * epwm6.c
 *
 *  Created on: 2021年1月20日
 *      Author: 78677
 */
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"

void Epwm6_Init(Uint16 time){
    EALLOW;
    // 失能  TBCLKSYNC：不让ePWM模块时钟工作
    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
    // 使能epwm6
    SysCtrlRegs.PCLKCR1.bit.EPWM6ENCLK = 1;
    // 引脚复用为EPWM6A
    GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 1;
    // 上拉引脚
    GpioCtrlRegs.GPAPUD.bit.GPIO10 = 0;

    // 引脚复用为EPWM6B
    GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 1;
    // 上拉引脚
    GpioCtrlRegs.GPAPUD.bit.GPIO11 = 0;
    EDIS;
    // 失能时钟同步 SYNCI
    EPwm6Regs.TBCTL.bit.SYNCOSEL =TB_SYNC_DISABLE;
    // 失能同步每个计时器 SYNCO
    EPwm6Regs.TBCTL.bit.PHSEN = TB_DISABLE;
    // 相位偏移寄存器,设置为0
    EPwm6Regs.TBPHS.half.TBPHS = 0;
    // 32位计数器，清空计数器
    EPwm6Regs.TBCTR = 0x0000;
    // 装载值
    EPwm6Regs.TBPRD = time;
    // 向上计数
    EPwm6Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP;
    // 不分频
    EPwm6Regs.TBCTL.bit.HSPCLKDIV=TB_DIV4;
    EPwm6Regs.TBCTL.bit.CLKDIV=TB_DIV4;
    EPwm6Regs.TBCTL.bit.PRDLD=0;
    // 设置影子寄存器加载值的方式
    EPwm6Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
    EPwm6Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
    // 当TB模块输入值为0时，影子寄存器开始将值加载到比较寄存器里
    EPwm6Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
    EPwm6Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;
    // 设置比较值
    EPwm6Regs.CMPA.half.CMPA = 0;  // A
    EPwm6Regs.CMPB = 0; //  B

    // 设置动作条件
    EPwm6Regs.AQCTLA.bit.ZRO = AQ_CLEAR; // Set PWM1A on Zero

    EPwm6Regs.AQCTLA.bit.CAU = AQ_SET; // Clear PWM1A on event A, up count
    EPwm6Regs.AQCTLB.bit.ZRO = AQ_SET; // Set PWM1B on Zero
    EPwm6Regs.AQCTLB.bit.CBU = AQ_CLEAR; // Clear PWM1B on event B, up count

    // 死区模块
//    EPwm6Regs.DBCTL.all=0xb;          // EPWMxB is inverted
//    EPwm6Regs.DBRED=1000; //（死区设置）
//    EPwm6Regs.DBFED=1000; //（死区设置）

    // PC 模块
//    EPwm6Regs.PCCTL.bit.OSHTWTH = 1000;
//    EPwm6Regs.PCCTL.bit.CHPFREQ = 7;
//    EPwm6Regs.PCCTL.bit.CHPDUTY = 7;
//    EPwm6Regs.PCCTL.bit.CHPEN = 1; // 使能PC模块

    // // 配置事件触发器
    EPwm6Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO; // Select INT on Zero event
    EPwm6Regs.ETSEL.bit.INTEN = 1; // Enable INT
      // 每发生一次触发事件就输出 PWM。
    EPwm6Regs.ETPS.bit.INTPRD = ET_3RD; 
    // 打开epwm时钟
    EALLOW;
    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // Start all the timers synced
    EDIS;
}

void Epwm6A_SetCompare(Uint16 val){
    Uint16 duty = (int)(9375* (val/100.0));
    EPwm6Regs.CMPA.half.CMPA = duty;  //设置占空比
}

void Epwm6B_SetCompare(Uint16 val){
    Uint16 duty = (int)(9375 * (val/100.0));
    EPwm6Regs.CMPB = duty;  //设置占空比
}
int a=0;

__interrupt void EPWM6_ISR(void){
    if(a>65535) a=0;
    a++;
    EPwm6Regs.ETCLR.bit.INT = 1; // 清除中断标志位
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}

void main(){
    InitSysCtrl();
    DINT;
    InitPieCtrl();
    InitPieVectTable();
    IER = 0x0000;
    IFR = 0x0000;
    EALLOW;
    PieVectTable.EPWM6_INT = &EPWM6_ISR;
    EDIS;
    PieCtrlRegs.PIEIER3.bit.INTx6 = 1;
    IER |= M_INT3;
    EnableInterrupts();
    Epwm6_Init(9375);   // 频率：1000Hz
    Epwm6A_SetCompare(50);
    Epwm6B_SetCompare(50);
    while(1){}
}

